package com.doform.android.xianggucheng.activity;

import android.text.TextUtils;
import android.widget.EditText;
import android.widget.TextView;

import com.doform.android.xianggucheng.R;
import com.doform.android.xianggucheng.bean.ClientCategoryBean;
import com.doform.android.xianggucheng.retrofit.subscriber.ErrorHandleSubscriber;
import com.doform.android.xianggucheng.retrofit.HttpManager;
import com.doform.android.xianggucheng.utils.Constans;
import com.doform.android.xianggucheng.utils.TsUtils;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.annotations.NonNull;

/**
 * 作者 Y_MS
 * Created by ${APEN} on 2017/6/7.
 * GitHub：https://github.com/cxydxpx
 */

public class UpdateClientCategoryActivity extends BaseTitleActivity {

    @BindView(R.id.et_name)
    EditText et_name;

    @BindView(R.id.et_sort)
    EditText et_sort;

    @BindView(R.id.tv_name)
    TextView tv_name;

    @BindView(R.id.tv_sort)
    TextView tv_sort;

    @OnClick(R.id.btn_submit)
    public void btn_submit() {
        beforeSubmit();
    }

    private ClientCategoryBean.ListBean mBean;

    @Override
    protected int layoutResId() {
        return R.layout.activity_common_sort;
    }

    @Override
    protected void init() {
        ClientCategoryBean.ListBean bean = (ClientCategoryBean.ListBean) getIntent().getExtras().getSerializable(Constans.EXTRA_UPDATE_CLIENT_CATEGORY);
        this.mBean = bean;
    }

    @Override
    protected void showView() {
        titleBar.setTextTitle(getResources().getString(R.string.client_category_update_title));

        tv_name.setText(getResources().getString(R.string.client_category_tv_name));
        tv_sort.setText(getResources().getString(R.string.default_tv_sort));

        et_name.setHint(getResources().getString(R.string.client_category_name_hint));
        et_sort.setText(getResources().getString(R.string.default_et_sort));

        et_name.setText(mBean.getName());
        et_sort.setText(mBean.getCorder());

        et_sort.requestFocus();
        et_name.requestFocus();
    }

    private void beforeSubmit() {

        String mName = et_name.getText().toString().trim();
        String mSort = et_sort.getText().toString().trim();

        if (TextUtils.isEmpty(mName)) {
            TsUtils.showToast("请输入客户分类名称");
            return;
        } else if (TextUtils.isEmpty(mSort)) {
            mSort = "255";
        }
        updateDataForService(mName, mSort);
    }

    private void updateDataForService(String mName, String mSort) {
        HttpManager.instance()
                .updateClientCategory(mBean.getId(),mBean.getGuid(), mName, mSort)
                .subscribe(new ErrorHandleSubscriber<String>() {
                    @Override
                    public void onNext(@NonNull String str) {
                        finish();
                    }
                });
    }
}
